package bidsanity

import org.junit.*
import grailserrors.ErrorCodes

class BidIntegrationTests {

    def s = new Customer(emailAddress: 'seller@example.org', password: "testing")
    def w = new Customer(emailAddress: 'winner@example.org', password: "testing")

    @Before
    void setUp() {
        // Setup logic here
        s.save()
        w.save()

    }

    @After
    void tearDown() {
        // Tear down logic here
    }

    @Test
    void testMultipleBids() {

        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s,
                winner: w)

        l.addToBids(new Bid(
                bidder: s,
                bidTime: new Date() - 10,
                amount: 1.0D,
                listing: l))

        l.save(failOnError: false, validate: true, flush: true)
        Assert.assertFalse l.hasErrors()
        Assert.assertEquals l.bids.size(), 1

        l.addToBids(new Bid(
                bidder: s,
                bidTime: new Date() - 10,
                amount: 1.5D,
                listing: l))

        l.save(failOnError: false, validate: true)

        Assert.assertFalse l.hasErrors()
        Assert.assertEquals l.bids.size(), 2
    }


    @Test
    void testAddBidTooLow() {

        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 10.0D,
                description: 'a test listing',
                seller: s,
                winner: w)
        //a good bid
        l.addToBids(new Bid(
                bidder: s,
                bidTime: new Date() - 10,
                amount: 15.0D,
                listing: l))
        l.save(failOnError: false, validate: true, flush: true)
        Assert.assertFalse l.hasErrors()

        //this bid is lower than bid 1
        l.addToBids(new Bid(
                bidder: s,
                bidTime: new Date() - 10,
                amount: 5.0D,
                listing: l))
        l.save(failOnError: false, validate: true, flush: true)
        Assert.assertTrue l.hasErrors()
    }
}
